﻿/***
 * 
 *           主题： 语言国际化 
 *    Description: 
 *           功能： 使得我们发布的游戏，可以根据不同的国家，显示不同的语言信息。
 * 
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SevenShine
{
    public class LanguageMgr
    {
        //本类实例
        public static LanguageMgr _Instance;
        //语言翻译的缓存集合
        private Dictionary<string, string> _DicLauguageCache;

        private LanguageMgr()
        {
            _DicLauguageCache = new Dictionary<string, string>();
            //初始化语言缓存集合
            InitLauguageCache();
        }

        /// <summary>
        /// 得到本类实例
        /// </summary>
        /// <returns></returns>
        public static LanguageMgr GetInstance()
        {
            if (_Instance == null)
            {
                _Instance = new LanguageMgr();
            }
            return _Instance;
        }

        /// <summary>
        /// 到显示文本信息
        /// </summary>
        /// <param name="lauguageID">语言的ID</param>
        /// <returns></returns>
	    public string ShowText(string lauguageID)
        {
            string strQueryResult = string.Empty;           //查询结果

            //参数检查
            if (string.IsNullOrEmpty(lauguageID)) return null;

            //查询处理
            if (_DicLauguageCache != null && _DicLauguageCache.Count >= 1)
            {
                _DicLauguageCache.TryGetValue(lauguageID, out strQueryResult);
                if (!string.IsNullOrEmpty(strQueryResult))
                {
                    return strQueryResult;
                }
            }

            Debug.Log(GetType() + "/ShowText()/ Query is Null!  Parameter lauguageID: " + lauguageID);
            return null;
        }

        public bool IsEnglish()
        {
            if (PlayerPrefs.GetInt("Language", 0) == 0)
            {
                SystemLanguage sysLan = Application.systemLanguage;
                if (sysLan == SystemLanguage.Chinese || sysLan == SystemLanguage.ChineseSimplified || sysLan == SystemLanguage.ChineseTraditional)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else if (PlayerPrefs.GetInt("Language") == 1)
            {
                return false;
            }
            else if (PlayerPrefs.GetInt("Language") == 2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void SwitchLanguage()
        {
            _DicLauguageCache.Clear();

            InitLauguageCache();
        }

        /// <summary>
        /// 初始化语言缓存集合
        /// </summary>
        private void InitLauguageCache()
        {
            IConfigManager config = null;

            //0表示未进行设置则默认显示与系统一样的语言，1表示中文  2表示英语
            if (PlayerPrefs.GetInt("Language", 0) == 0)
            {
                SystemLanguage sysLan = Application.systemLanguage;
                if (sysLan == SystemLanguage.Chinese || sysLan == SystemLanguage.ChineseSimplified || sysLan == SystemLanguage.ChineseTraditional)
                {
                    config = new ConfigManagerByJson("Json/LanguageJSONChinese");
                }
                else
                {
                    config = new ConfigManagerByJson("Json/LanguageJSONEnglish");
                }
            }
            else if (PlayerPrefs.GetInt("Language") == 1)
            {
                config = new ConfigManagerByJson("Json/LanguageJSONChinese");
            }
            else if (PlayerPrefs.GetInt("Language") == 2)
            {
                config = new ConfigManagerByJson("Json/LanguageJSONEnglish");
            }
             
            if (config != null)
            {
                _DicLauguageCache = config.AppSetting;
            }
        }
    }
}